package com.car.controller;

import com.alibaba.fastjson.JSON;
import com.car.entity.SysUser;
import com.car.pojo.User;
import com.car.pojo.carRoad;
import com.car.service.CarRoadService;
import com.car.util.*;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author VULCAN
 * @create 2020-03-09 17:25
 * @desc 轨迹
 **/
@CrossOrigin
@RequestMapping("road")
@Controller
public class CarRoadController {
    @Autowired
    private CarRoadService carRoadService;
    @Autowired
    RedisTemplate redisTemplate;
   /**
     * @name  列表展示
    * @Author VULCAN
    * @Date 2020/3/13 16:03
    */
    @ResponseBody
    @RequestMapping(value = "getcountList",method = RequestMethod.GET)
    public List<carRoad> getRoadList(){
        List<carRoad> count=carRoadService.getRoadcount();
        return count;
    }
    /**
      * @name  轨迹回放
     * @Author VULCAN
     * @Date 2020/3/13 16:03
     */
    @RequestMapping(value = "getPoint",method = RequestMethod.GET)
    public String getPoint(@RequestParam String id, Model model) throws Exception {
        String key = "dufy20170329java";
        String iv = "dufy20170329java";
        String encrypt = AesEncryptUtil.desEncrypt(id,key,iv);
        List<carRoad> list = carRoadService.getCarRoadList(encrypt);
        model.addAttribute("locals",list);

        return "video";
    }
    /**
      * @name  跳转到列表页面
     * @Author VULCAN
     * @Date 2020/3/13 16:10
     */
    @RequestMapping(value = "admin")
    public String tozhuce(){
        return "abc";
    }
    /**
      * @name  RES解密
     * @Author VULCAN
     * @Date 2020/3/13 16:03
     */
    @ResponseBody
    @RequestMapping(value = "res",method = RequestMethod.POST)
    public Result aes(@RequestBody carRoad carRoad) throws Exception {
        System.out.println(carRoad.getId());
        String id = carRoad.getId();
        try {
            //私钥解密
            PrivateKey privateKey1 = RSAUtil.getPrivateKey("MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAINzCPJI/hPrX/yd4pEPONfDupTV\n" +
                    "DU+3sys5LPm67UaYgZPSxvGxzVljRhfssG9KUH0R06NWRV++A0z+yyYEpDD9xVGErljMR1QZDC++\n" +
                    "9tiX3t2Jh9N+vnYNWh5F0qWh1Ke4qZIGk7iM46gR7BGo8GXL0dQdyWZAmjRNSKD0PM9vAgMBAAEC\n" +
                    "gYAzIEDwi3dXJAs3Y+lFZlhDg3tEfAEralWjkB9wGkZDWPm9FxQN2Yv3ImeW0pZlEtBvdMmOE/Xz\n" +
                    "oSIDhm5ZISEC5LhB+t8LR4mkFN4bfxTu8HKqoKtMHfXDz2EoPIPRulkLTJ9tgF8KEf9La/47oUnt\n" +
                    "SeNWtWd+BNETim0Y7WbxUQJBANWHQkRxPTpQBMniFKRTgzEFd/tUDxSPe/Cmw2Xw28wuP2A4xlGF\n" +
                    "Za8VoNcJmn+I0G7uo/xA9Xd3CWq6CB5qYDMCQQCdmFxPjeSwRF1dUswNb99M6/c6Yf9+/n2vn0ro\n" +
                    "wnOKx9Wy3KNLrYDVZR+Pyv0FjhVGOUrCCmefjDcfXzhLoSfVAkEAmjh07kXzePhuXPmC+ySuLmvK\n" +
                    "uqV9ttXjKG7p1ejed1w3veGDq0Fzrb8rSeTPx6kjEdweaITqRXyeOo1ea8lc7QJAMtUZOWPoVt7G\n" +
                    "SrrRLKhgG3ylMwS3F6xYuBQmYmuOPz5z9Ixsc5WUT8CdbJEqCeepfwwty+b1Q6ZDhW/+RY7GvQJB\n" +
                    "ALfrdzOegdiTNcuSZNPX6PqVUPeTBg67RG7ofEd+zs9OILTZU5ldUA583zBNxwCHBjwx3VlPOqzM\n" +
                    "1CngQZZ1prw=");
            String  oldSource = RSAUtil.decryptString(privateKey1, id);
            System.out.println("解密后数据:" + oldSource);
            HttpUrlConnectionToInterface.doPostOrGet("http://localhost:8088/road/getroadinsert",oldSource, "POST");
            return  new Result(true,"解密及上传成功");
        } catch (Exception e) {
            return  new Result(false,"解密失败");
        }
    }
    /**
      * @name  提供接口
     * @Author VULCAN
     * @Date 2020/3/13 16:03
     */
    @ResponseBody
    @RequestMapping(value = "batchInsert",method = RequestMethod.POST)
    public Result batchInsert(@RequestBody List<carRoad> carRoads) {
        try {
            carRoadService.batchInsert(carRoads);
            return  new Result(true,"上传成功");
        }catch (Exception e){
            return  new Result(false,"上传失败");
        }
    }
    /**
      * @name  上报平台
     * @Author VULCAN
     * @Date 2020/3/13 16:03
     */
    @ResponseBody
    @RequestMapping(value = "getRoadId",method = RequestMethod.GET)
    public String getRoadId(@RequestParam("id") String id) throws Exception {
//        String key = "dufy20170329java";
//        String iv = "dufy20170329java";
//        String encrypt = AesEncryptUtil.desEncrypt(id,key,iv);
        List<carRoad> roadId = carRoadService.getRoadId(id);
        String s = JSON.toJSONString(roadId);
        String post = HttpUrlConnectionToInterface.doPostOrGet("http://30h07f0830.qicp.vip/batchInsert", s, "POST");
//        http://30h07f0830.qicp.vip/batchInsert
        return post;
    }
    /**
      * @name  自调接口
     * @Author VULCAN
     * @Date 2020/3/13 16:03
     */
    @ResponseBody
    @RequestMapping(value = "Api")
    public String Api() throws Exception {
        List<carRoad> roadId = carRoadService.getRoadId("1");
        String s = JSON.toJSONString(roadId);
        String encript = null;
        try {
            //公钥加密
            PublicKey publicKey1 = RSAUtil.getPublicKey("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCDcwjySP4T61/8neKRDzjXw7qU1Q1Pt7MrOSz5uu1GmIGT0sbxsc1ZY0YX7LBvSlB9EdOjVkVfvgNM/ssmBKQw/cVRhK5YzEdUGQwvvvbYl97diYfTfr52DVoeRdKlodSnuKmSBpO4jOOoEewRqPBly9HUHclmQJo0TUig9DzPbwIDAQAB");
            encript = RSAUtil.encryptString(publicKey1, s);
            System.out.println("加密后数据：" + encript);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String post = HttpUrlConnectionToInterface.doPostOrGet("http://localhost:8088/road/res", "{" + "\t\"id\":\"" + encript + "\"\n" +
                "}", "POST");
        return post;
    }
    @ResponseBody
    @RequestMapping(value = "admins",method = RequestMethod.POST)
    public Result amin(@RequestBody User user) throws Exception {
        SysUser amin = carRoadService.amin(user);
        if (amin != null){
            String key = "dufy20170329java";
            String iv = "dufy20170329java";
            String encrypt = AesEncryptUtil.encrypt(amin.getUsername() + amin.getPassword(), key, iv);
            redisTemplate.delete("encrypt");
            redisTemplate.opsForValue().set("encrypt",encrypt);
            return new Result(true,"登录成功",encrypt);
        }else{
            return new Result(false,"登录失败");
        }


    }
}
